package jcommon.chap9_templating.freemarker;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import jcommon.chap9_templating.freemarker.bean.Enrollment;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Demo1 {

	public static void main(String[] args) {
		StringWriter writer = new StringWriter( );

		// Create a Configuration object for FreeMarker
		Configuration cfg = Configuration.getDefaultConfiguration( );
		cfg.setTemplateLoader(new ClassTemplateLoader(Demo1.class));
		cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);

		// The root Map serves as a Context for our template engine
		Map root = new HashMap( );
		root.put("enrollment", getTestEnrollment( ));

		// A template is processed with a Map and output is sent to a Writer.
		Template template;
		try {
			template = cfg.getTemplate("demo1.ftl");
			template.process(root, writer);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (TemplateException e) {
			throw new RuntimeException(e);
		}
		System.out.println("output: \n" + writer.toString( ));
	}

	private static Object getTestEnrollment() {
		return new Enrollment() ;
	}
	
	
}
